package br.com.fogas.utils;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

public class StringUtility extends StringUtils {
	
	public static final String FORMATO_MOEDA_SIMBOLO = "$ ###,###,##0.00";
	
	public static final Locale LOCALE_BRASIL = new Locale("pt", "BR");
	
	public static final String UTF8 = Charset.forName("UTF-8").displayName();
	
	public String currencyFormat(double value) {
		DecimalFormatSymbols simboloReal = new DecimalFormatSymbols(LOCALE_BRASIL);
		DecimalFormat formatoReal = new DecimalFormat(FORMATO_MOEDA_SIMBOLO, simboloReal);
		formatoReal.setMaximumFractionDigits(2);
		formatoReal.setMinimumFractionDigits(2);

		String s = formatoReal.format(value).trim();
		if (s.startsWith("$")) {
			s = "R" + s;
		} else if (s.startsWith("-$")) {
			s = "R$ -" + s.substring(2);
		}
		if (s.startsWith("-$")) {
			s = s.replace("-$", "-R$");
		}
		return s;
	}
	
	public String cnpjFormatado(String cnpj) {
		//05518915000298
		//03.016.262/0001-32
		if(cnpj.toCharArray().length == 14) {
			String cnpjTemp = "";
			cnpjTemp += substring(cnpj, 0, 2) + ".";
			cnpjTemp += substring(cnpj, 2, 5) + ".";
			cnpjTemp += substring(cnpj, 5, 8) + "/";
			cnpjTemp += substring(cnpj, 8, 12) + "-";
			cnpjTemp += substring(cnpj, 12, 14);
			return cnpjTemp;
		}
		return "";
	}
	
	public String substringBetweenPreserve(String string, String tag1, String tag2) {
		string = StringUtility.substringBetween(string, tag1, tag2);
		string = tag1 + string + tag2;
		
		return normalizeSpace(string);
	}
	
	public String getSubstringByRegex(final String regex, final String string) throws RuntimeException {
		
		Pattern pattern = Pattern.compile(regex, Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(string);
		
		String substringByRegex = EMPTY;
		while (matcher.find()) {
			substringByRegex = matcher.group(1);
		}
		
		if (isEmpty(substringByRegex)) {
			throw new RuntimeException("substring by regex not found");
		} else {
			return normalizeSpace(substringByRegex);
		}
	}
	
	public String getStringByInputStream(final InputStream stream) throws IOException {
		
		if(stream == null) {
			throw new RuntimeException();
		}
		
		final List<String> contents = IOUtils.readLines(stream, UTF8);

		StringBuilder lines = new StringBuilder();
		
		for (String line : contents) {
			lines.append(line);
		}
		
		return normalizeSpace(lines.toString());
	}
	
	/**M�todo que retorna <b>Codigo do Fog�s Entrega</b> sem os zeros a esquerda.
	 * @author andre.nascimento
	 * @param String - partner (0002161148)
     * @return String - partner (2161148)
     */
	public static String removeZeroEsquerdaString(final String partner) {
		
		int index = 0;
		
		for (int i =0; i< partner.length(); i++) {
		    try {
		    	String strChar = String.valueOf(partner.charAt(i));
		    	Integer intChar = Integer.parseInt(strChar);
		    	
		    	if(!intChar.equals(0)) {
		    		index = i;
		    		break;
		    	}
		    	
		   } catch (NumberFormatException ex) {
			   ex.printStackTrace();
		   }
		}
		
		return StringUtility.substring(partner, index, partner.length());
	}
}
